﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />

$.ajaxSetup({
    url: "/Handler/ProjectAward.ashx?" + (new Date()).valueOf(),
    type: "POST",
    dataType: "text",
    global: false,
});

$(document).ready(function () {
    $("#btnSave").bind("click", Save);
    debugger;
    var ID = $("#MainContent_ID").val();
    if (!Util.isEmpty(ID)) {
        BindData(ID);
    }
});

function Save() {    
    if(!IsValidate())
    {
        return;
    }
    var ret=GetData();
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:Util.isEmpty(ret.ID)?"addData":"updateData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
        }
    });
}

function GetData() {
    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val()),
       "Name": encodeURIComponent($("#Name").val()),
       "Memo":encodeURIComponent($("#Memo").val()),
       "LastModifiedBy":encodeURIComponent($("#MainContent_UserID").val()),
    }

    return ret;
}

function IsValidate() {
    var msg="";
    if (Util.isEmpty($('#Name').val())) {
        msg="名称不能为空!";
    }

    if(Util.isEmpty(msg))
    {
        return true;
    }
    else
    {
        alert(msg);
        return false;
    }
}

function BindData(ID,Category) {
    $.ajax({
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData",id:ID},
            beforeSend: function () {
                Util.blockUI("正在加载数据...");
            },
            success: function (data) {
                Util.unblockUI();
                var obj=$.parseJSON(data).ProjectAwards[0];
                $("#MainContent_ID").val(decodeURIComponent(obj.ID));
                $("#Name").val(decodeURIComponent(obj.Name));
                $("#Memo").val(decodeURIComponent(obj.Memo));
            }
        });
}